//
//  NetWorkService.m
//  DailyRecord
//
//  Created by Gin on 2022/3/27.
//

#import "NetWorkService.h"

static NSDictionary *publicHeaders = nil;

static NSArray *jsonNames = nil;

@implementation NetWorkService

+ (void)initialize{
    publicHeaders = [self publicHeaders];
    jsonNames = @[kUserRegister, kAddData];
}

+ (Boolean)isInJsonRequestSerializerWhiteNames:(NSString *)path{
    Boolean contained = NO;
    for (NSString *inPath in jsonNames) {
        if ([inPath isEqualToString:path] || [path hasPrefix:inPath]) {
            contained = YES;
            break;
        }
    }
    return contained;
}

+ (NSDictionary *)publicHeaders{
    return @{
        @"app_key":@"fa64f288df1a91d42c8271809156e834YYp",
        @"app_sign":@"8f8a9c962dedae25f7ce0cc97972e6d1"
    };
}

+ (NSString *)getFullUrlWithPath:(NSString *)path{
    NSString *url = [NSString stringWithFormat:@"%@/%@", kBaseUrl, path];
    return url;
}

+ (NSString *)getOriginalAndCurrentWithTask:(NSURLSessionDataTask *)task{
    NSURLRequest *ori = [task originalRequest];
    NSURLRequest *cur = [task currentRequest];
    NSString *str = [NSString stringWithFormat:@"ori == %@, cur == %@", ori.URL.absoluteString, cur.URL.absoluteString];
    return str;
}

+ (void)parseResponseIsSuccess:(NSDictionary *)res
                          success:(ServiceRequestSuccessBlock)success
                          failure:(ServiceRequestFailBlock)failure{
    NSString *code = [NSString stringWithFormat:@"%@",[res objectForKey:kRequestResponseCodeKey]];
    Boolean isSuccess = [code isEqualToString:kRequestSuccessCode];
    if (isSuccess) {
        if (success) {
            success(res);
        }
    }else{
        NSError *error = [NSError errorWithDomain:@"" code:100110 userInfo:nil];
        if (failure) {
            failure(error);
        }
    }
}

+ (void)logRequestSuccessWithReq:(NSURLSessionDataTask *)task responseObj:(id)responseObj{
#if DEBUG
    NSLog(@"%@ success, responseObj: %@", [self getOriginalAndCurrentWithTask:task], responseObj);
#endif
}

+ (void)logRequestFailWithReq:(NSURLSessionDataTask *)task error:(NSError *)error{
#if DEBUG
    NSLog(@"%@ fail, error: %@", [self getOriginalAndCurrentWithTask:task], error);
#endif
}

+ (void)serviceGetWithPath:(NSString *)path
              parameters:(id)parameters
                 success:(ServiceRequestSuccessBlock)success
                 failure:(ServiceRequestFailBlock)failure{
    [NetWorkManager getWithURLString:[self getFullUrlWithPath:path] parameters:parameters headers:[self publicHeaders] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObj) {
        [self logRequestSuccessWithReq:task responseObj:responseObj];
        [self parseResponseIsSuccess:responseObj success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        [self logRequestFailWithReq:task error:error];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)servicePostWithPath:(NSString *)path
               parameters:(id)parameters
                  success:(ServiceRequestSuccessBlock)success
                  failure:(ServiceRequestFailBlock)failure{
    NetWorkManagerrequestSerializerType type = [self isInJsonRequestSerializerWhiteNames:path] ? NetWorkManagerrequestSerializerTypeJson : NetWorkManagerrequestSerializerTypeForm;
    [NetWorkManager postWithURLString:[self getFullUrlWithPath:path] requestSerializerType:type parameters:parameters headers:[self publicHeaders] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObj) {
        [self logRequestSuccessWithReq:task responseObj:responseObj];
        [self parseResponseIsSuccess:responseObj success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        [self logRequestFailWithReq:task error:error];
        if (failure) {
            failure(error);
        }
    }];
}

@end
